package com.hg.library.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.jakewharton.disklrucache.DiskLruCache;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;


/**
 * Created by ASUS-PC on 2016/4/9.
 */
public class DiskCacheUtils {
    private DiskCacheUtils() {
    }

    private static DiskLruCache diskLruCache = null;

    private static final int MAX_CACHE_SIZE = 50 * 1024 * 1024;

    public static void build(Context context) {
        try {
            diskLruCache = DiskLruCache.open(new File(context.getCacheDir() + File.separator + "bitmap"), 1, 1, MAX_CACHE_SIZE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static DiskLruCache getInstance() {
        return diskLruCache;
    }

    public static void put(String key, Bitmap bitmap) {
        try {
            DiskLruCache.Editor edit = diskLruCache.edit(EncryptUtils.castToMD5(key));
            if (edit != null) {
                OutputStream outputStream = edit.newOutputStream(0);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
                outputStream.flush();
                outputStream.close();
                edit.commit();
                diskLruCache.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Bitmap get(String key) {
        try {
            DiskLruCache.Snapshot snapshot = diskLruCache.get(EncryptUtils.castToMD5(key));
            if (snapshot == null) return null;
            else return BitmapFactory.decodeStream(snapshot.getInputStream(0));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void put(String key, Object o) {
        try {
            DiskLruCache.Editor edit = diskLruCache.edit(EncryptUtils.castToMD5(key));
            if (edit != null) {
                OutputStream outputStream = edit.newOutputStream(0);
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                objectOutputStream.writeObject(o);
                objectOutputStream.close();
                outputStream.flush();
                outputStream.close();
                edit.commit();
                diskLruCache.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static <T> T getObj(String key) {
        try {
            DiskLruCache.Snapshot snapshot = diskLruCache.get(EncryptUtils.castToMD5(key));
            if (snapshot == null) return null;
            else {
                ObjectInputStream inputStream = new ObjectInputStream(snapshot.getInputStream(0));
                return (T) (inputStream.readObject());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
